package chapter01.principle.lsp;

//错误示例：子类破坏父类行为
class Rectangle {
    protected int width, height;
    public void setWidth(int w) { width = w; }
    public void setHeight(int h) { height = h; }
    public int getArea() {
        return width * height;
    }
    public static void main(String[] args) {
        Rectangle rectangle = new Rectangle();
        rectangle.setWidth(5);
        rectangle.setHeight(10);
        System.out.println("Rectangle area: " + rectangle.getArea()); // 预期输出 50

        Rectangle square = new Square();
        square.setWidth(5);
        square.setHeight(10);
        System.out.println("Square area: " + square.getArea()); // 预期输出 50，但实际输出 100
    }
}

class Square extends Rectangle {
    // 正方形设置边长时需同时修改宽高
    public void setWidth(int w) {
        super.setWidth(w);
        super.setHeight(w); // 违反父类行为
    }
}
